/**
 * LinkedLST, Kellen Donohue
 * May 5, 2011
 * RailsModelParser.java - Runner class to update the POJOs
 */
package edu.washington.cs.labspecimentransport.data;

import java.io.File;
import java.io.FileFilter;

/**
 * @author Kellen
 * 
 */
public class RailsModelParser {

	/**
	 * @param args
	 *            Optionally, the inputFile and outputDir as command line args,
	 *            otherwise defaults will be used
	 * @throws Exception
	 *             If the schema rb file has a model that can't be de-pluralized
	 */
	public static void main(String[] args) throws Exception {
		String inputFile, outputDir;
		if (args.length < 2) {
			inputFile = "../rails/db/schema.rb";
			// TODO: Make it file.separatorChar
			outputDir = "src/edu/washington/cs/labspecimentransport/dataobjects/";
		} else {
			inputFile = args[0];
			outputDir = args[1];
		}

		// Delete all the old models
		File dir = new File(outputDir);
		for (File oldFile : dir.listFiles(new FileFilter() {
			// Only delete the java files -- goal files end in .goal
			public boolean accept(File pathname) {
				return pathname.toString().endsWith(".java");
			}
		})) {
			oldFile.delete();
		}

		SchemaRbParser schemaParser = new SchemaRbParser(inputFile);
		for (RailsModel model : schemaParser.parseFile()) {
			model.writeModel(outputDir);
		}
	}
}
